package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.operamask.OperaMaskSceneData;
import com.yanqu.road.entity.activity.peakstronghold.CrossUnionAbilityCacheData;
import com.yanqu.road.entity.activity.peakstronghold.UserPeakStrongholdPatronsData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionAbilityCacheDaoImpl extends TempDataObjectDao<CrossUnionAbilityCacheData> {
    public static String getCrossUnionAbilityCacheDataSql = "select * from t_c_union_ability_cache where activityId=?";

    public Map<String, CrossUnionAbilityCacheData> getCrossUnionAbillityCacheData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, CrossUnionAbilityCacheData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUnionAbilityCacheDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionAbilityCacheData data = parse(rs);
                dataMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossUnionAbilityCacheDataSql = "update t_c_union_ability_cache set userAbility=? where activityId=? and unionId=?";

    @Override
    protected boolean updateData(CrossUnionAbilityCacheData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserAbility())));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        if (getDb().executeNoneQuery(updateCrossUnionAbilityCacheDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossUnionAbilityCacheDataSql = "insert into t_c_union_ability_cache(activityId,unionId,userAbility) values (?,?,?)";

    @Override
    protected boolean addData(CrossUnionAbilityCacheData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserAbility())));
        if (getDb().executeNoneQuery(insertCrossUnionAbilityCacheDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossUnionAbilityCacheData parseData(ResultSet rs) throws SQLException {
        CrossUnionAbilityCacheData data = new CrossUnionAbilityCacheData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionId(rs.getString("unionId"));
        data.setUserAbility(JSON.parseObject(rs.getString("userAbility"), new TypeReference<ConcurrentHashMap<Long, BigInteger>>() {
        }));

        return data;
    }
}
